#import "RNEditListItemViewController.h"
#import "RNEditItemBackgroundView.h"
#import "RNStepper.h"
#import "RNStepperCell.h"
#import "RNConstants.h"
#import "RNItem.h"
#import "RNAisle.h"
#import "RNStore.h"
#import "RNCategory.h"
#import "RNListItem.h"
#import "NSManagedObjectContext+SRAdditions.h"
#import "NSTreeController+SRAdditions.h"
#import "DetailViewController.h"

static CGFloat const RNEditItemNotesViewMinimumHeight = 28.0f;

#pragma mark -
@interface RNEditListItemViewController (RNEditItemViewControllerPrivateMethods)
-(void)updateViewFrame_;
-(NSTextView *)utilityTextView_;
@end


#pragma mark -
@implementation RNEditListItemViewController

@synthesize notesView = notesView_;
@synthesize stepper = stepper_;
@synthesize notesField = notesField_;

-(void)awakeFromNib {
    [super awakeFromNib];
    [[self notesView] setBackgroundColor:[NSColor colorWithCalibratedRed:0.981f green:0.977f blue:0.93f alpha:1.0f]];
    [[self titleField] setFont:[NSFont systemFontOfSize:12.0f]];
    NSRect stepperFrame = [[self stepper] frame];
    stepperFrame.size.height = 14.0f;
    [[self stepper] setFrame:stepperFrame];
    [[[self stepper] cell] setFontSize:11.0f];
    [[[self stepper] cell] setTextColor:[NSColor colorWithCalibratedRed:0.402 green:0.410 blue:0.467 alpha:1.000]];
    [[[self stepper] cell] setArrowColor:[NSColor colorWithCalibratedRed:0.500 green:0.510 blue:0.582 alpha:1.000]];
    [[[self stepper] cell] setArrowPressedColor:[NSColor colorWithCalibratedRed:0.316 green:0.331 blue:0.375 alpha:1.000]];

//    [[self stepper] setFont:[NSFont systemFontOfSize:9.0f]];
    [[[self stepper] cell] setSendsActionOnEndEditing:NO];
    [[[self notesField] cell] setSendsActionOnEndEditing:NO];
    
//    NSSortDescriptor *leafDescriptor = [[NSSortDescriptor alloc] initWithKey:@"isLeaf" ascending:YES];
}

-(void)updateViewFrame_ {
    NSRect editViewFrame = [[self view] frame];
    CGFloat newHeight = [self preferredHeightForWidth:NSWidth(editViewFrame)] + (2 * RNEditListItemShadowRadius);
    if (newHeight == NSHeight(editViewFrame))
        return;
    editViewFrame.origin.y -= newHeight - NSHeight(editViewFrame);
    editViewFrame.size.height = newHeight;
    [[self view] setFrame:editViewFrame];
//    NSLog(@"setting height: %f", NSHeight(editViewFrame));
}

-(NSTextView *)utilityTextView_ {
    if (!utilityTextView_) 
        utilityTextView_ = [[NSTextView alloc] initWithFrame:NSZeroRect];
    return utilityTextView_;
}

-(CGFloat)preferredNotesFieldSizeForWidth:(CGFloat)aWidth {
    NSTextView *textView = (NSTextView *)[[self notesField] currentEditor];
    if (!textView) {
        textView = [self utilityTextView_];
        [textView setString:[[self notesField] stringValue]];
        [textView setFrameSize:NSMakeSize(aWidth, RNEditItemNotesViewMinimumHeight)];
        [[textView layoutManager] glyphRangeForTextContainer:[textView textContainer]];
    }
    return NSHeight([[textView layoutManager] usedRectForTextContainer:[textView textContainer]]);
}

-(CGFloat)preferredHeightForWidth:(CGFloat)aWidth {
    CGFloat notesViewWidth = NSWidth([[self notesView] frame]);
    CGFloat notesFieldWidth = NSWidth([[self notesField] frame]);
    CGFloat notesFieldPadding = notesViewWidth - notesFieldWidth;
    CGFloat preferredNotesHeight = [self preferredNotesFieldSizeForWidth:(aWidth - notesFieldPadding)];
    if (preferredNotesHeight < RNEditItemNotesViewMinimumHeight)
        preferredNotesHeight = RNEditItemNotesViewMinimumHeight;
    CGFloat currentNotesHeight = NSHeight([[self notesField] frame]);
    CGFloat notesHeightDelta = preferredNotesHeight - currentNotesHeight;
    
    CGFloat currentViewHeight = NSHeight([[self view] frame]);
    CGFloat preferredViewHeight = currentViewHeight + notesHeightDelta;
//    preferredViewHeight += (2 * RNEditListItemShadowRadius);
    return preferredViewHeight;
}

//-(void)hideEditItemView {
//    id editItem = [self editingItem];
//    if (!editItem)
//        return;
//    
//    [[self view] removeFromSuperview];
//    [self setEditingItem:nil];
//    [[NSNotificationCenter defaultCenter] postNotificationName:RNEditItemViewDidHideNotification object:editItem];
//}


#pragma mark -
#pragma mark Notifications

- (void)controlTextDidChange:(NSNotification *)aNotification {
    [self updateViewFrame_];
}


//#pragma mark -
//#pragma mark Actions
//
//-(IBAction)closeEditItemView:(id)sender {
//    [self hideEditItemView];
//}

// TODO: TRY IMPLEMENTING SHOULDBEGINEDITING, GET THE FIELD EDITOR, MOVE INSERTION POINT TO END OF LINE

@end
